package cn.jdy.platform.tag;

import cn.jdy.framework.util.SpringContextUtils;
import cn.jdy.platform.entity.News;
import cn.jdy.platform.service.NewsService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * 文章类型的自定义标签
 * @description 将文章，新闻这种常规性的功能通过自定义标签后获取内容
 * @author zhaigt
 * @date 2015年6月26日
 */
public class NewsListTag extends SimpleTagSupport implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	private String var;
	private boolean pid=false;// 父栏目id
	private Long columnid;// 栏目id
	private int limitMax=50; //默认一次最多查询记录数
	private String style;// 显示样式
	
	private boolean img=false;// 是否过滤封面图片
	private int statuses=0;//加入状态

	@Override
	public void doTag() throws JspException, IOException {
	  NewsService newsService = SpringContextUtils.getBean(NewsService.class);
	  
		List<News> list = null;
		if(pid){// 按照父分类查询
		  list = newsService.findbyPid(columnid, 0, limitMax, img);
		}else{// 按照栏目查询
		  list = newsService.findbytype(columnid, 0, limitMax, img);
		}
		if((!pid)&&(statuses==1)){//状态为1的时候查询状态加入置顶
			list = newsService.findbytypeNstatus(columnid, 0, limitMax, img, statuses);
		}
		if((!pid)&&(statuses==2)){//状态为2的时候查询状态为1有效、3置顶的news中imgurl有值的前limitmax
			list = newsService.findbyNstatus(columnid, 0, limitMax, img, statuses);
		}
		Iterator<News> ite = list.iterator();
		// 进行迭代
		while (ite.hasNext()) {
			Object obj = ite.next();
			getJspContext().setAttribute(var, obj);
			//输出标签体
			getJspBody().invoke(null);
		}
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}
	


  public int getLimitMax() {
    return limitMax;
  }

  public void setLimitMax(int limitMax) {
    this.limitMax = limitMax;
  }

  public Long getColumnid() {
    return columnid;
  }

  public void setColumnid(Long columnid) {
    this.columnid = columnid;
  }

  public boolean getPid() {
    return pid;
  }

  public void setPid(boolean pid) {
    this.pid = pid;
  }

  public boolean getImg() {
    return img;
  }

  public void setImg(boolean img) {
    this.img = img;
  }

public int getStatuses() {
	return statuses;
}

public void setStatuses(int statuses) {
	this.statuses = statuses;
}
 


}
